<?php

/**
* Milestones Controller
*/
class BatchesController extends AppController
{
	var $name = 'Batches';
	var $components = array('UserAuth', 'ContentRight');
	var $project_id = null;
	var $uses = array ('Batch', 'Milestone', 'User');
	var $helpers = array('Ajax','Time');
	
	public function beforeFilter()
	{
		if (!empty($this->params['projectId'])) {
			$this->set('project_id', $this->params['projectId']);
			$this->set('project_name', $this->params['projectName']);
			$this->project_id = $this->params['projectId'];
		} else {
			$this->set('project_id', 0);
			$this->set('project_name', "");
		}
	}
	
	function index()
	{
		if ($this->project_id == null) {
			$this->redirect('/');
		}
		$this->UserAuth->checkPermission($this->project_id);
		$this->Batch->unbindModel(array('belongsTo' => array('Project')));
		$results = $this->Batch->findAllByProjectIdAndFinished($this->project_id, '0');
		$this->set ('object', $results);
	}

	public function view($batch_id)
	{
		if ($this->project_id == null) {
			$this->redirect('/');
		}
		$this->UserAuth->checkPermission($this->project_id);
		$results = $this->Batch->findByIdAndProject_id($batch_id, $this->project_id);
		if (empty($results)) {
			// Le lot n'existe pas.
			$this->Session->setFlash("Le lot n'existe pas");
		}
		$this->set ('batch', $results);
		$milestones = array();
		foreach($results['Milestone'] as $milestone) {
			$tickets = array();
			foreach($results['Ticket'] as $ticket) {
				if ($ticket['milestone_id'] == $milestone['id']) {
					$tickets[] = $ticket;
				}
			}
			$milestones[] = array('id' => $milestone['id'],  'name' => $milestone['name'], 'finished' => $milestone['finished'], 'deadline' => $milestone['deadline'], 'Ticket' => $tickets);
		}
		$this->set ('tickets', $results['Ticket']);
		$this->set ('name', $results['Batch']['name']);
		$this->set ('description', $results['Batch']['description']);
		$this->set ('milestones', $milestones);
	}
	
	public function add()
	{
		if (empty($this->data) == false) {
			if ($this->Batch->save($this->data['Batch'])) {
				$id = $this->Batch->getLastInsertId();
				$batch = $this->data['Batch'];
				$milestones = $this->data['Milestone'];
				for($i = 0; !empty($milestones[$i]); $i++) {
					if ($milestones['check_' . $i] == 0) {
						continue;
					}
					$this->Milestone->id = $milestones[$i];
					$milestone_src = $this->Milestone->read();
					$milestone = array('id'=> 'NULL',
										'name' => $milestone_src['Milestone']['name'],
										'batch_id' => $id,
										'deadline' => $milestones['deadline_'.$i.'_year']
										.'-'.$milestones['deadline_'.$i.'_month']
										.'-'.$milestones['deadline_'.$i.'_day']);
					$this->Milestone->save($milestone);
				}
                $this->Session->setFlash('Your batch has been saved.');
				$this->redirect ('/' . $batch['project_name'] . '/' . $batch['project_id'] . '/batches/view/' . $id);
			} else {
				$this->Milestone->unbindModel(array('belongsTo' => array('Batch')));
				$milestones = $this->Milestone->findAllByBatchId(0, NULL, 'position');
				$this->set('default_milestones', $milestones);
			}
		} else {
			if (!empty($this->params['projectId'])) {
				$this->set('project_id', $this->params['projectId']);
				$this->set('project_name', $this->params['projectName']);
				$project_id = $this->params['projectId'];
				$this->Milestone->unbindModel(array('belongsTo' => array('Batch')));
				$milestones = $this->Milestone->findAllByBatchId(0, NULL, 'position');
				$this->set('default_milestones', $milestones);
			} else {
				$this->redirect('/');
			}
		}
	}

}

?>